<template>
  <div class="user-page">
    <el-breadcrumb class="breadcrumb page-title" separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">User</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="container-padding">
      <!--serchBox-->
      <div class="search-box">
        <el-form :inline="true" label-position="top" :model="searchForm" size="mini" ref="ruleForm"
                 class="demo-ruleForm main-search-box">

          <el-form-item prop="region" :label="$t('m.Status')" style="width: 200px;margin-right: 0">
            <el-select style="width: 200px" v-model="searchForm.statusId" placeholder="">
              <el-option v-for="rg in userStatus" :key="rg.value" :label="rg.label" :value="rg.value"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item prop="region" :label="$t('m.Restaurant')" style="width: 200px;margin-left: 20px">
            <el-input v-model="searchForm.restaurantName" placeholder=""></el-input>
          </el-form-item>

          <el-form-item style="margin-top: 37px;margin-left: 30px;">
            <el-button  @click="getList(1)">{{$t('m.Search')}}</el-button>
            <el-button  @click="newUser()">{{$t('m.New')}}</el-button>
          </el-form-item>
        </el-form>
      </div>

      <!--list-->
      <div class="list-items-box">
        <!--表格-->
        <el-table
          :data="tableData"
          v-loading="tableLoading"
          border
          @selection-change="handleSelectionChange"
          style="width: 100%">

          <el-table-column
            prop="Property"
            label="Edit"
            align="center"
            width="100">
            <template slot-scope="scope">
              <p>edit</p>
            </template>
          </el-table-column>

          <!--Status-->
          <el-table-column
            prop="Property"
            label="Status"
            align="center"
            min-width="100">
            <template slot-scope="scope">
              <el-button @click="view(scope.row.transactionId)" type="text">ID:{{scope.row.listingId}}</el-button>
              <div>{{getTransactionItem(scope.row.transactionTypeId)}}</div>
              <p>{{getPropertyTypeItem(scope.row.propertyTypeId)}}</p>
            </template>
          </el-table-column>

          <!--RestaurantName-->
          <el-table-column
            prop="Price"
            label="RestaurantName"
            align="center"
            min-width="140">
            <template slot-scope="scope">
              <p v-if="scope.row.mmkPrice">{{scope.row.mmkPrice}} (Lakh)</p>
              <p v-if="scope.row.usdPrice">{{scope.row.usdPrice}} (USD)</p>
            </template>
          </el-table-column>

          <!--Region-->
          <el-table-column
            prop="Address"
            label="Region"
            align="center"
            min-width="120">
            <template slot-scope="scope">
              <p>{{getRegionItem (scope.row.regionId)}}</p>
            </template>
          </el-table-column>

          <!--Township-->
          <el-table-column
            prop="Address"
            label="Township"
            align="center"
            min-width="120">
            <template slot-scope="scope">
              <p>{{getTownshipItem (scope.row.townshipId)}}</p>
            </template>
          </el-table-column>

          <!--Street-->
          <el-table-column
            prop="Address"
            label="Street"
            align="center"
            min-width="120">
            <template slot-scope="scope">
              <p>{{scope.row.street}}</p>
            </template>
          </el-table-column>


          <!--UserName-->
          <el-table-column
            prop="Address"
            label="UserName"
            align="center"
            min-width="120">
            <template slot-scope="scope">
              <p>{{scope.row.street}}</p>
            </template>
          </el-table-column>

          <!--Email-->
          <el-table-column
            prop="Address"
            label="Email"
            align="center"
            min-width="120">
          </el-table-column>

          <!--Phone-->
          <el-table-column
            prop="Phone"
            label="Phone"
            align="center"
            min-width="120">
          </el-table-column>

          <!--Notes-->
          <el-table-column
            prop="Notes"
            label="Notes"
            align="center"
            min-width="120">
          </el-table-column>


          <!--CreatedDate-->
          <el-table-column
            prop="Date"
            label="CreatedDate"
            align="center"
            min-width="120">
            <template slot-scope="scope">
              <p>{{$t('m.Created')}}: {{getRoomDate(scope.row.createdDate)}}</p>
            </template>
          </el-table-column>

          <!--CreatedBy-->
          <el-table-column
            prop="CreatedBy"
            label="CreatedBy"
            align="center"
            min-width="120">
          </el-table-column>

          <!--UpdatedDate-->
          <el-table-column
            prop="Date"
            label="UpdatedDate"
            align="center"
            min-width="120">
            <template slot-scope="scope">
              <p>{{$t('m.Created')}}: {{getRoomDate(scope.row.createdDate)}}</p>
            </template>
          </el-table-column>

          <!--UpdatedBy-->
          <el-table-column
            prop="Notes"
            label="UpdatedBy"
            align="center"
            min-width="120">
            <template slot-scope="scope">
              <div class="list-notes">{{scope.row.notes}}</div>
            </template>
          </el-table-column>
        </el-table>
        <!--分页-->
        <div class="block" style="margin-top: 15px;text-align: center;">
          <el-pagination
            @size-change="handleChangeSize"
            @current-change="handleChangePage"
            :current-page="pageData.pageNo"
            :page-size="pageData.pageSize"
            :page-sizes="[10, 20, 50, 100, 500]"
            :page-count="pageInfo.totalPages"
            :total="pageInfo.totalCount"
            layout="total, prev, pager, next, jumper, sizes">
          </el-pagination>
        </div>
      </div>
    </div>

    <!--新增user弹出层-->
    <el-dialog
      title="New"
      :visible.sync="userModalShow"
      :before-close="userModalShowClose"
      class="new-user-dialog"
    >
      <el-form label-position="top" ref="newuser" :rules="rulesWithEmail">

        <el-form-item label="Restaurant Name">
          <el-select style="width: 100%" v-model="NewRestaurantForm.restaurantName" placeholder="">
            <el-option
              v-for="item in restaurantNameOption"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="User Name" prop="userName">
          <el-input v-model="NewRestaurantForm.userName"></el-input>
        </el-form-item>

        <el-form-item label="Email" prop="email">
          <el-input v-model="NewRestaurantForm.email"></el-input>
        </el-form-item>

        <el-form-item label="Phone">
          <el-input v-model="NewRestaurantForm.phone"></el-input>
        </el-form-item>

      </el-form>

      <span slot="footer" class="dialog-footer">
             <el-button @click="userModalShowClose">Cancel</el-button>
             <el-button type="primary"
                        @click="submitUserFormSubmit">Save</el-button>
        </span>
    </el-dialog>


    <!--编辑user弹出层-->
    <el-dialog
      title="Edit"
      :visible.sync="userModalShowEdit"
      :before-close="userModalShowCloseEdit"
      class="new-user-dialog"
    >
      <el-form label-position="top" ref="edituser" :rules="rulesWithEmail">

        <el-form-item label="Restaurant Name">
          <el-select disabled style="width: 100%" v-model="NewRestaurantFormEdit.restaurantName" placeholder="">
            <el-option
              v-for="item in restaurantNameOption"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="Status">
          <el-select  style="width: 100%" v-model="NewRestaurantFormEdit.restaurantStatus" placeholder="">
            <el-option
              v-for="item in restaurantStatusEdit"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="Name" prop="userName">
          <el-input v-model="NewRestaurantFormEdit.userName"></el-input>
        </el-form-item>

        <el-form-item label="Email" prop="email">
          <el-input v-model="NewRestaurantFormEdit.email"></el-input>
        </el-form-item>

        <el-form-item label="Notes (write reason for changes)" prop="notes">
          <el-input
            type="textarea"
            :rows="2"
            placeholder=""
            v-model="NewRestaurantFormEdit.Notes">
          </el-input>
        </el-form-item>

      </el-form>

      <span slot="footer" class="dialog-footer">
             <el-button @click="userModalShowClose">Cancel</el-button>
             <el-button type="primary"
                        @click="submitUserFormSubmit">Save</el-button>
        </span>
    </el-dialog>
  </div>
</template>

<script>
  // 英文json
  import propertyType from '@/json/propertyType'
  import propertySubType from '@/json/propertySubType'
  import regions from '@/json/region'
  import townships from '@/json/township'
  import buySort from '@/json/buySort'

  // 缅甸文json
  import mm_propertyType from '@/mmJson/propertyType'
  import mm_propertySubType from '@/mmJson/propertySubType'
  import mm_regions from '@/mmJson/region'
  import mm_townships from '@/mmJson/township'
  import mm_buySort from '@/mmJson/buySort'

  // import { searchPropertyList, propertyStatusCount, agentDevelopmentStatus} from '../../api/api'

  export default {
    name: 'Properties',
    data() {
      return {
        restaurantNameOption: [{
          value: '选项1',
          label: '黄金糕'
        }, {
          value: '选项2',
          label: '双皮奶'
        }, {
          value: '选项3',
          label: '蚵仔煎'
        }, {
          value: '选项4',
          label: '龙须面'
        }, {
          value: '选项5',
          label: '北京烤鸭'
        }],
        restaurantStatusEdit: [
          {
            value: '0',
            label: 'Active'
          },
          {
            value: '1',
            label: 'Suspended'
          },
        ],
        statusArr: [
          {statusId: 2, countNumber: 0},
          {statusId: 1, countNumber: 0},
          {statusId: 4, countNumber: 0},
        ],
        // 搜索子段
        townships: [],
        propertySubType: [],
        searchForm: {
          statusId: '',
          restaurantName: ''
        },
        // 新增user字段
        NewRestaurantForm:{
          restaurantName: '',
          userName: '',
          email: '',
          phone: ''
        },
        // 编辑user字段
        NewRestaurantFormEdit: {
          restaurantName: '',
          restaurantStatus: '',
          userName: '',
          email: '',
          Notes: ''
        },
        pageData: {
          pageNum: 1,
          pageSize: 20,
        },
        pageInfo: {
          totalCount: 0,
          totalPages: 0
        },
        tableData: [],
        sendData: {
          transactionStatusTypeId: 2,
          propertyTypeId: ''
        },
        rulesWithEmail: {
          email: [
            {required: true, message: 'Please enter the mailbox address', trigger: 'change'},
            {type: 'email', message: 'Please enter the correct mailbox address', trigger: 'change'}
          ],
          userName: [
            {required: true, message: 'Please enter user Name', trigger: 'change'},
          ],
          notes: [
            {required: true, message: 'Please enter user Notes', trigger: 'change'},
          ],
        },
        userModalShow: false,
        tableLoading: false,
        userModalShowEdit: false,
      }
    },
    computed: {
      townshipJson() {
        if (this.$store.state.localLang == 'MM') {
          return mm_townships
        } else {
          return townships
        }
      },
      regions() {
        if (this.$store.state.localLang == 'MM') {
          return mm_regions
        } else {
          return regions
        }
      },
      userStatus() {
        if (this.$store.state.localLang == 'MM') {
          return [
            {"text": "ဌားရန္", "value": 1},
            {"text": "ေရာင္းရန္", "value": 2},
          ]
        } else {
          return [
            {"text": "Active", "value": 1},
            {"text": "Not interested", "value": 2},
            {"text": "Business stopped", "value": 3},
            {"text": "Suspended", "value": 4},
            {"text": "Deleted", "value": 5},
          ]
        }
      },
    },
    methods: {
      getTownships(regionId) {
        let town = []
        this.townships = []
        this.searchForm.townshipId = []

        if (this.$store.state.localLang == 'MM') {
          for (var t of mm_townships) {
            if (regionId === t.regionId) {
              town.push({value: t.value, text: t.text})
            }
          }
        } else {
          for (var t of townships) {
            if (regionId === t.regionId) {
              town.push({value: t.value, text: t.text})
            }
          }
        }
        this.townships = town
      },
      getTownshipItem(it) {
        let str = ''
        this.townshipJson.forEach((item) => {
          if (item.value == it) {
            str = item.text || ''
          }
        })
        return str
      },
      getRegionItem(it) {
        let str = ''
        this.regions.forEach((item) => {
          if (item.value == it) {
            str = item.text || ''
          }
        })
        return str
      },
      getSubType(type) {
        // 首先处理部分dom 显示隐藏

        // 初始化
        // var arrShow = [
        //   {"value": 1, "text": "Apartment", "showland": false},
        //   {"value": 2, "text": "Condo", "showland": false},
        //   {"value": 3, "text": "House", "showland": true},
        //   {"value": 4, "text": "Land", "showland": true, "showOther": true},
        //   {"value": 8, "text": "Commercial", "showland": true}
        // ]

        if (this.searchForm.propertyTypeId == 1 || this.searchForm.propertyTypeId == 2) {
          this.landSizeShow = false
        } else {
          this.landSizeShow = true
        }

        if (this.searchForm.propertyTypeId == 4) {
          this.showOther = false
        } else {
          this.showOther = true
        }

        // 将more中数据初始化
        this.moreForm = {
          totalBedrooms: [],
          masterBedroom: '',
          Bathroom: '',
          floorSizeList: [
            {minSize: '', maxSize: ''}
          ],
          landSizeList: [
            {minSize: '', maxSize: ''}
          ],
          floorLevelId: [],
          FurnishingId: [],
          BuildingAgeID: [],
          FacingID: [],
          SourceId: ''
        };

        let subType = []
        this.propertySubType = []
        this.searchForm.propertySubTypeId = []

        if (this.$store.state.localLang == 'MM') {
          for (var t of mm_propertySubType) {
            if (type === t.propertyTypeId) {
              subType.push({value: t.value, text: t.text})
            }
          }
        } else {
          for (var t of propertySubType) {
            if (type === t.propertyTypeId) {
              subType.push({value: t.value, text: t.text})
            }
          }
        }
        this.propertySubType = subType
      },
      getTransactionItem(it) {
        let str = ''
        this.transaction.forEach((item) => {
          if (item.value == it) {
            str = item.text || ''
          }
        })
        return str
      },
      getPropertyTypeItem(it) {
        let str = ''
        this.propertyType.forEach((item) => {
          if (item.value == it) {
            str = item.text || ''
          }
        })
        return str
      },
      getPropertySubTypeItem(it) {
        let str = ''
        this.propertySubTypeJson.forEach((item) => {
          if (item.value == it) {
            str = item.text || ''
          }
        })
        return str
      },
      handleSelectionChange(val) {
        this.multipleSelection = val
        // [{row当前所有数据，developmentId}]
      },
      handleChangeSize(val) {
        this.pageData.pageSize = parseInt(val)
        this.pageData.pageNum = 1
        this.getList()
      },
      handleChangePage(val) {
        this.pageData.pageNum = val
        this.getList()
      },
      dateFormat(value, str) {
        var date = new Date(value)
        var Y = date.getFullYear()
        var m = date.getMonth() + 1
        var d = date.getDate()
        var H = date.getHours()
        var i = date.getMinutes()
        var s = date.getSeconds()
        if (m < 10) {
          m = '0' + m
        }
        if (d < 10) {
          d = '0' + d
        }
        if (H < 10) {
          H = '0' + H
        }
        if (i < 10) {
          i = '0' + i
        }
        if (s < 10) {
          s = '0' + s
        }
        if (str == 'listedDate') {
          var t = d + '-' + m + '-' + Y + '  ' + H + ':' + i + ':' + s
          return t
        } else {
          var ts = m + '-' + Y
          return ts
        }
      },
      getRoomDate(value) {
        if (!value) {
          return ''
        }
        var date = new Date(value)
        var Y = date.getFullYear()
        var m = date.getMonth() + 1
        var d = date.getDate()
        if (m < 10) {
          m = '0' + m
        }
        if (d < 10) {
          d = '0' + d
        }
        var ts = d + '-' + m + '-' + Y
        return ts
      },
      userModalShowClose(){
        // 重置
        this.NewRestaurantForm = {
          restaurantName: '',
          userName: '',
          email: '',
          phone: ''
        }
        this.userModalShow = false;
      },
      userModalShowCloseEdit(){

      },
      submitUserFormSubmit(){},
      newUser(){
        this.userModalShow = true;
      },
      getList(pageNum) {

        if (pageNum == 1) {
          this.pageData.pageNum = 1;
        }

        let searchForm = {};

        // searchPropertyList(Object.assign(searchForm, this.pageData)).then((res) => {
        //   this.tableData = res.data.object.list || []
        //   // 分页信息配置
        //   this.pageInfo.totalPages = res.data.object.pages
        //   this.pageInfo.totalCount = res.data.object.total
        //   window.scrollTo(0, 0)
        // }).catch((e) => {
        //   console.log(e)
        // })
      },
    },
    mounted() {
      this.getList()
    }
  }
</script>


<style>
  .page-title {
    padding: 20px 40px;
    border-bottom: 1px solid #e6e6e6;
    font-size: 20px !important;
  }

  .breadcrumb {
    margin-bottom: 20px;
  }

  .page-title {
    padding: 20px 40px;
    border-bottom: 1px solid #e6e6e6;
    font-size: 20px;
  }

  .search-box {
    position: relative;
    /*padding-top: 20px;*/
    border-bottom: 1px solid #eee;
  }

  .line-form {
    border-top: 1px solid #eee;
  }

  .more-box, .list-items-box {
    padding-top: 20px;
    padding-bottom: 10px;
  }

  .list-items-box {
    /*margin-right: 10%;*/
  }

  .list-items-box .el-form--inline.el-form--label-top .el-form-item__content {
    display: inline-block;
  }

  .change-status {
    font-size: 14px;
    margin-right: 10px;
  }

  .options {
    padding: 0px 0 20px 0;
  }

  .container-padding {
    padding: 0 40px;
  }

  .list-notes {
    display: -webkit-box;
    -webkit-line-clamp: 4;
    -webkit-box-orient: vertical;
    overflow: hidden;
    line-height: 1.5;
  }

  .new-user-dialog .el-form-item {
    margin-bottom: 0!important;
  }
</style>
